package com.yuwei.service;

import com.yuwei.bean.RetMessage;
import com.yuwei.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired UserService userService;
    public RetMessage login(User user){
        System.out.println("用户名:" + user.getUserName() + ",密码:" + user.getUserPwd());
        final String userName = user.getUserName();
        final String userPwd = user.getUserPwd();
        String pwd = userService.getUserPwdByName(userName);
        if(pwd !=null && pwd.equals(userPwd)) {
            return createMessage(true);
        }
        return createMessage(false);
    }

    private RetMessage createMessage(boolean isSuccess){
        RetMessage retMsg = new RetMessage();
        if(isSuccess){
            retMsg.setStatus(1);
            retMsg.setMsg("用户登录成功");
            //登录成功设置token
            retMsg.setToken("yuwei");
        }else{
            retMsg.setStatus(0);
            retMsg.setMsg("用户登录失败");
        }
        return retMsg;
    }
}
